package com.czy.seed.mvc.home.mapper;

import com.czy.seed.mvc.home.entity.*;
import com.czy.seed.mybatis.base.mapper.BaseMapper;
import com.czy.seed.mybatis.config.mybatis.annotations.AutoMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;


@AutoMapper
public interface PurchaseDetailedListMapper extends BaseMapper<PurchaseDetailedList> {
    @Update(" update purchase_detailed_list  set type=#{type},updatetime=sysdate(),updateuserid=#{c_userid}  where ssdd_glgx=#{ssdd_glgx}")
    void  updateByParentId(@Param("type") String  type, @Param("ssdd_glgx") long  ssdd_glgx,@Param("c_userid") long  c_userid);
    @Update(" update purchase_detailed_list  set type=#{type},updatetime=sysdate(),updateuserid=#{c_userid}   where id=#{id}")
    void  updateById(@Param("type") String  type, @Param("id") long  id,@Param("c_userid") long  c_userid);
    @Update(" update purchase_detailed_list  set type=#{type},updatetime=sysdate(),updateuserid=#{c_userid} ,resultvalidationname=#{resultvalidationname},resultvalidationpath=#{resultvalidationpath}  where id=#{id}")
    void  updateByParentId_(@Param("type") String  type, @Param("id") long  id,@Param("resultvalidationname") String  resultvalidationname,@Param("resultvalidationpath") String  resultvalidationpath,@Param("c_userid") long  c_userid);
    @Update(" update purchase_detailed_list  set type=#{type},updatetime=sysdate(),updateuserid=#{c_userid} ,memorandinunitingname=#{resultvalidationname},memorandinunitingpath=#{resultvalidationpath}  where id=#{id}")
    void  updateByParentId_m(@Param("type") String  type, @Param("id") long  id,@Param("resultvalidationname") String  resultvalidationname,@Param("resultvalidationpath") String  resultvalidationpath,@Param("c_userid") long  c_userid);
    @Select("\tSELECT DISTINCT\n" +
            "\t\t\tpurchase_detailed_list.id\n" +
            "\t\tFROM purchase_detailed_list\n" +
            "\t\tINNER JOIN quote_or_compare ON quote_or_compare.PURCHASE_ID = purchase_detailed_list.ID\n" +
            "\t\tINNER JOIN quotation ON quotation.QUOTE_OR_COMPARE_ID = quote_or_compare.ID\n" +
            "\t\tWHERE\n" +
            "\t\t\tquote_or_compare.ID =#{id}")
     long selectPurchaseOrdesIdByQuotation(@Param("id") long  id);
    @Select("select (case when a.num is null then 0 else a.num end)num from(\n" +
            "SELECT\n" +
            "(sum(purchase_detailed_list.PMMC)=null)num\n" +
            "FROM\n" +
            "purchase_detailed_list\n" +
            "INNER JOIN purchase_orders ON purchase_detailed_list.SSDD_GLGX = purchase_orders.ID\n" +
            "INNER JOIN seed_sys_user ON purchase_orders.CREATOR_USERID = seed_sys_user.ID\n" +
            "inner join merchant on merchant.ID=purchase_detailed_list.MERCHANTID\n" +
            "WHERE\n" +
            "(seed_sys_user.ID =#{id} or\n" +
            "seed_sys_user.CREATE_BY =#{id} or\n" +
            "merchant.USER_ID =#{id}) and (\n" +
            "purchase_detailed_list.RESULTVALIDATIONPATH is null or \n" +
            "purchase_detailed_list.MEMORANDINUNITINGPATH is null) and purchase_detailed_list.TYPE<>'申请' and purchase_detailed_list.TYPE<>'待审核' and purchase_detailed_list.TYPE<>'作废' and purchase_detailed_list.TYPE<>'申请作废')a")
    public int selectSum(@Param("id") long  id);
    @Update(" update purchase_detailed_list  set purchase_detailed_list.MERCHANTID=#{mmerchant_id},updatetime=sysdate(),updateuserid=#{c_userid},purchase_detailed_list.ZJ=#{QuoteSum},purchase_detailed_list.TYPE='结果确认'   where id=#{id}")
    void  updateByParentId4mmerchant(@Param("mmerchant_id") long  mmerchant_id, @Param("id") long  id,@Param("c_userid") long  c_userid,@Param("QuoteSum")BigDecimal QuoteSum);



    @Select("SELECT \n" +
            "purchase_detailed_list.DJ as univalence,\n" +
            "purchase_detailed_list.ZJ as total,\n" +
            "purchase_detailed_list.SL as num,\n" +
            "purchase_detailed_list.GOODS_TYPE as GoodsType,\n" +
            "date_format(purchase_detailed_list.UPDATETIME,'%Y-%m') as updateTime,\n" +
            "commodity.COMMODITY_MAK_PRC as MAK_PRC \n" +
            " FROM purchase_detailed_list  \n" +
            " LEFT JOIN commodity ON \n" +
            " \n" +
            " purchase_detailed_list.COMMODITY_ID = commodity.id \n" +
            " WHERE  purchase_detailed_list.TYPE = '已完成'"
            )
    /**
     * 平台数据统计分析
     * liuyun
     */
     List<StatisticsDataVo> statisticsData();


}
